﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Comm;
using Entities;
using BusinessFacade;

namespace MedicareNoteManagerWeb.Web
{
    public partial class UserRole :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadDrp();
                LoadList();
            }
        }
        public void LoadDrp()
        {
            FillDropDownList("UserGetList", "UserID", "LoginName", "请选择用户", DropUser);
        }
        public void LoadList()
        {
            DataTable dt = new Busi_sys_Role().GetListByUserID(int.Parse(DropUser.SelectedValue));
            Grd_User.DataSource = dt;
            Grd_User.DataBind();
        }

        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            LoadList();
        }

        protected void Grd_User_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Grd_User.EditIndex = -1;
            LoadList();
        }

        protected void Grd_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int ID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
                new BusinessFacade.Busi_sys_Role().DeleteRoleUser(ID);
                MessageBox.ShowDialog(Page, "删除成功！", "success");
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "删除失败！" + ex.Message, "error");
                LoadList();
            }
        }

        protected void Grd_User_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Grd_User.EditIndex = e.NewEditIndex;
            LoadList();
        }

        protected void Grd_User_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                sys_user_Model model1 = (sys_user_Model)Session["UserModel"];
                sys_userrole_Model model = new sys_userrole_Model();
                model.UserRoleID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
                model.UserID = Convert.ToInt32(((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("ddlRole")).SelectedValue);
                model.RoleID = Convert.ToInt32(((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("ddlMoudel")).SelectedValue);
                model.ModifyUserID = model1.UserID;
                model.ModifyDate = DateTime.Now;
                new BusinessFacade.Busi_sys_Role().UpdateUserRole(model);
                MessageBox.ShowDialog(Page, "更新成功！", "success");
                Grd_User.EditIndex = -1;
                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('更新成功！'); </script>");
                LoadList();
            }
            catch (System.Exception ex)
            {
                MessageBox.ShowDialog(Page, "更新失败！" + ex.Message, "error");
                Grd_User.EditIndex = -1;
                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('更新失败！" + ex.Message + "'); </script>");
                LoadList();
            }
        }

        protected void Grd_User_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList ddlRole = new DropDownList();
            ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
            DropDownList ddlMoudel = new DropDownList();
            ddlMoudel = (DropDownList)e.Row.FindControl("ddlMoudel");
            if (ddlRole != null && ddlMoudel != null)
            {
                FillDropDownList("UserGetList", "UserID", "LoginName", "请选择用户", ddlRole);
                FillDropDownList("RoleGetList", "RoleID", "RoleName", "请选择角色", ddlMoudel);
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                DataTable dt = new BusinessFacade.Busi_sys_Role().GetModelByUserID(Id);
                ddlMoudel.SelectedValue = dt.Rows.Count > 0 ? dt.Rows[0]["RoleID"].ToString() : "0";
                ddlRole.SelectedValue = dt.Rows.Count > 0 ? dt.Rows[0]["UserID"].ToString() : "0";
            }
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            LoadList();
        }
    }
}